Chapter 2. Input values
How to get a string in C programming Language
Define a "string"
char variable_name[50];
Without and specific length
If you don't want to specify the length of a string you can only put [] blank brackets:
char name[];
Using scanf() to input values
To get a string as you know in other programming languages, you have to define a char variable in C. All char variables can have a length, this length is next to the variable name:
char potatoe[8] = "potatoe";
#include <stdio.h>
int main () {
char name[50]; // this variable in reality has 49 chars plus one \n char
printf("Write your name !: ");
scanf("%s", &name); // This read the value entered by the user
print("The name is %s\n", name);
return 0;
}
Also you can write other datatypes, like integers or floating-point numbers:
#include <stdio.h>
int main () {
int a;
printf("Write a number please: ");
scanf("%i", &a);
return 0;
}
Also you can write two values using only one scanf:
#include <stdio.h>
int main () {
int a, b;
printf("Enter two numbers using a blank sapce between these");
scanf("%d %d", &a, &b);
return 0;
}
A disadvantage of using scanf() is that this method doesn't read blank spaces and break lines. To read these also, we have another method called fgets()
Using fgets()
#include <stdio.h>
int main() {
char fullName[100];
printf("Please, enter your full name: ");
fgets(fullName, sizeof(fullName), stdin); // This is the line which reads the value entered by the user
printf("Your full name is %s\n", fullName);
return 0;
}
Some important elements:
sizeof(): This method gets the length from a variable or datatype in bytes, in this case, we used that to get the exact size used fromfullNamevariable.
Security
In this point, the more secure method is fgets, because it prevents from overflow, also fgets accepts blank spaces and break lines.
End of a string
In C programming language, to represent the end of a string the character \0 is used to represent that, for example:
#include
int main() {
char phrase[] = "Hola desde Mexico";
for (int i = 0; phrase[i] != '\0'; i++) {
if(phrase[i+1] == '\0') {
printf("this is the end of this phrase\n");
}
}
return 0;
}
Inputs with and without &
Char & works as a pointer, wel something like that. In reality it is referencing the memory address of the variable called in the function scanf, because in this way, we can modify the value of the current varaible
If we use in scanf() the variable without &, it could represent a fatal error, the program will interpret that value as undefined, it could generate trash in memory and security bugs.
& returns the value in memory of that variable
char character;
scanf("%c", &character); // this is correct
If you have a list of characters (string), you can use only the name of the variable, because this variable already has a pointer char *.
So instead of use &, we only can use the name of the array, because these arrays already have a pointer *
char name[20];
scanf("%s", name); // this is correct
Print Values
Once you already have written a variable using & accessing to that space in memory, now you only can print using the name of that variable.
#include <stdio.h>
int main() {
int age;
printf("What is yor age: ");
scanf("%i", &age); // Note here we are using &
print("Your age is %d", age); // Now we are not using &
return 0;
}
Why?
This is because we already have a pointer using an array, * this pointer makes unnecessary makes another one.